package com.ter;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;


public class MyJTableModel extends AbstractTableModel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String titre[];
	private ArrayList<Object> data[];
	
	public MyJTableModel(String[] titre, ArrayList<Object>[] data) {
		super();
		this.titre = titre;
		this.data = data;
	}
	
	@SuppressWarnings("unchecked")
	public MyJTableModel(String[] titre) {
		super();
		this.titre = titre;
		data=new ArrayList[this.titre.length];
		for (int i = 0; i < data.length; i++) {
			data[i]=new ArrayList<Object>();
		}
	}
	
	@Override
	public int getColumnCount() {
		
		return titre.length;
	}
	
	@Override
	public int getRowCount() {
		
		return data[0].size();
	}
	
	@Override
	public Object getValueAt(int r, int c) {
		return data[c].get(r);		
	}
	
	public String getColumnName(int c) {
		return titre[c];
		
	}
	
	public void setValueAt(Object value, int r, int c){
		data[c].set(r, value);
	}
	
	public void addline(String s,String sep){
		String str[]=s.split(sep);		
		for (int i = 0; i < data.length; i++) {
			data[i].add(str[i]);
		}
		//fireTableStructureChanged();
		fireTableDataChanged();
	}
	
	public void addline(int index,String s,String sep){
		if(index>=0 && index<=getRowCount()){
			String str[]=s.split(sep);
			for (int i = 0; i < data.length; i++) {
				data[i].add(index,str[i]);
			}
			//fireTableStructureChanged();
			fireTableDataChanged();
		}
	}
	
	public void DeleteLine(int index){
		if(getRowCount()!=0 && index>=0 && index <getRowCount()){
			for (int i = 0; i < data.length; i++) {
				data[i].remove(index);
			}
			//fireTableStructureChanged();
			fireTableDataChanged();
		}
	}
	
	public void clearTable(){
		int nbLine=getRowCount();
		for (int i = nbLine-1; i >= 0; i--) {
			DeleteLine(i);
		}
	}
	
	@Override
	public boolean isCellEditable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}
	
	public ArrayList<Object>[] getData() {
		return data;
	}

	public void setData(ArrayList<Object>[] data) {
		this.data = data;
	}

}
